package com.jetpack.mvvm.lib.binding.viewbind

import android.app.Dialog
import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding
import com.jetpack.mvvm.lib.binding.base.DialogDelegate
import com.jetpack.mvvm.lib.binding.ext.inflateMethod
import kotlin.reflect.KProperty

class DialogViewBinding<T : ViewBinding>(
    clazz: Class<T>,
    lifeCycle: Lifecycle? = null
) : DialogDelegate<T>(lifeCycle) {
    private var layoutInflater = clazz.inflateMethod()
    override fun getValue(thisRef: Dialog, property: KProperty<*>): T {
        return viewBinding?.run {
            this
        } ?: let {
            val bind = layoutInflater.invoke(null, thisRef.layoutInflater) as T
            return bind.apply { viewBinding = this }
        }
    }

}